<template>
    <view class="content">
        <!-- <view v-for="(el, ind) in list" :key="ind"> -->
			<view class="box">
			    <view class="item" @click="show = true">
			        <view>选择库区</view>
			        <input type="text" v-model="houseName" placeholder="请选择" class="inp" disabled>
			        <u-icon name="arrow-down-fill" color="#a7a7a7" size="28" class="u-m-l-15"></u-icon>
			    </view>
			    <view class="item">
			        <view>选择货位</view>
			        <input type="text" v-model="library" class="inp" disabled>
			        <image src="../../static/icon/scan.png" class="scan-icon" @click="scanCodeLibrary"></image>
			    </view>
			    <view class="item">
			        <view>选择托盘</view>
			        <image src="../../static/icon/scan.png" class="scan-icon" @click="scanCodeTray"></image>
			    </view>
			    <view class="u-p-b-15">
			        <view class="tray text-m text-bold" v-for="(item, index) in tray" :key="index">
			            <view>{{item}}</view>
			            <u-icon name="close" color="#99a0af" size="35" @click="delTray(index)"></u-icon>
			        </view>
			    </view>
				<!-- 用验收合格数量做对比 -->
				<!-- <view class="item">
				    <view>入库数量</view>
				    <input type="text" placeholder="请输入入库数量" class="inp">
				</view> -->
				<!-- 向下取整 Math.floor(num) -->
				<!-- <view class="item">
				    <view>件数<text style="color: #fff;">件数</text> </view>
				    <input type="text" placeholder="自动计算" class="inp">
				</view> -->
			</view>
			
			<!-- <view class="btns">
			    <view class="del-btn" v-if="list.length > 1" @click="del(ind)">
			        <image src="../../static/icon/del.png" class="btn-icon"></image>
			        <view>删除</view>
			    </view>
			    <view class="add-btn" @click="add">
			        <image src="../../static/icon/add.png" class="btn-icon"></image>
			        <view>添加</view>
			    </view>
			</view> -->
		<!-- </view> -->
        
        <view class="box u-p-t-30 u-p-b-30">
            <view>备注</view>
            <textarea v-model="content" maxlength="-1" :auto-height="true" placeholder="请输入" class="t-inp"></textarea>
        </view>
        
        <view class="btn-square u-m-t-50" @click="$noMultipleClicks(doSubmit)">确认入库</view>
        
        <!-- 库区 -->
        <u-select v-model="show" mode="single-column" :list="house" value-name="id" label-name="name" @confirm="confirm"></u-select>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                noClick: true,//连点
                id: '',
                goods_id: '',
                show: false,
                house: [],//库区
				houseName: '',
				houseId: '',
				library_id: '',//货位
				library: '',//
				tray_ids: [],//托盘
				tray: [],//
				
                content: '',
				// list: [{
				// 	houseName: '',
				// 	houseId: '',
				// 	library_id: '',//货位
				// 	library: '',//
				// 	tray_ids: [],//托盘
				// 	tray: [],//
				// }],
            };
        },
        onLoad(option) {
            this.id = option.id
            this.goods_id = option.goods_id
            this.getHouse()
        },
        methods: {
			// 添加
			// add() {
			//     this.list.push({ })
			// },
			
			// // 删除
			// del(ind) {
			//     this.list.splice(ind, 1)
			// },
			
			
            doSubmit() {
                if (!this.houseId) {
                    this.$toast('请选择库区')
                    return
                }
                if (!this.library_id) {
                    this.$toast('请选择货位')
                    return
                }
                if (this.tray_ids.length <= 0) {
                    this.$toast('请选择托盘')
                    return
                }
                this.$ajax('inventory_put/put', {
                    userToken: this.$getSync('userToken'),
                    check_id: this.id,
                    goods_id: this.goods_id,
                    
                    house_id: this.houseId,//库区
                    house: this.houseName,
                    library_id: this.library_id,//货位
                    library: this.library,
                    tray_ids: this.tray_ids.join(','),//托盘
                    tray: this.tray.join(','),
                    // 数量  和验收合格数量 做对比
                    
                    memo: this.content,//备注
                }).then(ret => {
                	if (ret.status == 0) {
                        this.$toast('提交成功');
                        setTimeout(() => {
                            this.$backT()
                        }, 500)
                	} else {
                		this.$toast(ret.message);
                	}
                });
            },
            
            // 扫码 货位
            scanCodeLibrary() {
                if (!this.houseId) {
                    this.$toast('请先选择库区！')
                    return
                } else {
                    this.$scanCode((res) => {
                        console.log('000',res, res.result)
                        this.getLibrary(res.result)
                    })
                }
            },
            
            // 获取货位
            getLibrary(keywords) {
            	this.$ajax('index/get_library', {
                    userToken: this.$getSync('userToken'),
                    stores_house_id: this.houseId,//库区
                    keywords: keywords,
                }).then(ret => {
            		if (ret.status == 0) {
                        if (ret.data && ret.data[0]) {
                            let a = ret.data && ret.data[0]
                            this.library = a.name
                            this.library_id = a.id
                        } else {
                            this.$toast('该库区没有这个货位！')
                        }
            		} else {
            			this.$toast(ret.message);
            		}
            	});
            },
            
            // 扫码 托盘
            scanCodeTray() {
                this.$scanCode((res) => {
                    console.log('scanCodeTray',res, res.result)
                    this.getTray(res.result)
                })
            },
            
            // 删除托盘
            delTray(index) {
                this.tray_ids.splice(index, 1)
                this.tray.splice(index, 1)
            },
            
            // 获取 托盘
            getTray(keywords) {
            	this.$ajax('index/get_tray', {
                    userToken: this.$getSync('userToken'),
                    keywords: keywords,
                }).then(ret => {
            		if (ret.status == 0) {
                        if (ret.data && ret.data[0]) {
                            if (this.tray_ids && this.tray_ids.length <= 0) {
                                let b = ret.data && ret.data[0]
                                this.tray_ids.push(b.id)
                                this.tray.push(b.name)
                            } else {
                                let list = ret.data
                                this.tray_ids.forEach(cur => {
                                    list.forEach((el, eli) => {
                                        if (cur == el.id) {
                                            list.splice(eli, 1)
                                        }
                                    })
                                })
                                if (list&&list[0]&&list[0].id) {
                                    this.tray_ids.push(list[0].id)
                                    this.tray.push(list[0].name)
                                }
                            }
                        }
            		} else {
            			this.$toast(ret.message);
            		}
            	});
            },
            
            // 选择库区
            confirm(e) {
                console.log(e);
                this.houseName = e[0].label
                this.houseId = e[0].value
            },
            
            // 获取库区
            getHouse() {
            	this.$ajax('index/get_stores_house', {
                    userToken: this.$getSync('userToken'),
                }).then(ret => {
            		if (ret.status == 0) {
                        this.house = ret.data
            		} else {
            			this.$toast(ret.message);
            		}
            	});
            },
        }
    };
</script>

<style lang="scss">
	.btns{
	    padding: 20rpx 0 35rpx;
	    display: flex;
	    justify-content: flex-end;
	    align-items: center;
	    color: #fff;
	}
	.btns>view{
	    display: flex;
	    justify-content: center;
	    align-items: center;
	    padding: 15rpx 30rpx;
	    border-radius: 10rpx;
	}
	.btn-icon{
	    width: 24rpx;
	    height: 24rpx;
	    margin-right: 10rpx;
	}
	.del-btn{
	    background-color: #bfbfbf;
	}
	.add-btn{
	    background-color: #ff4800;
	    margin-left: 20rpx;
	}
	
    page{
        background-color: #f5f5f5 !important;
    }
    .content{
        padding: 30rpx;
    }
    .box{
        padding: 0 20rpx;
        background-color: #fff;
        border-radius: 15rpx;
    }
    .item{
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 30rpx 0;
        border-bottom: 2rpx solid #e6e6e6;
    }
    .item:last-child{
        border-bottom: none;
    }
    .inp{
        flex: 1;
        padding: 0 20rpx 0 50rpx;
    }
    .scan-icon{
        width: 37rpx;
        height: 37rpx;
    }
    .tray{
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 30rpx;
        background-color: #f5f7fa;
        color: #192b55;
        margin-bottom: 15rpx;
    }
    .t-inp{
        width: 100%;
        min-height: 150rpx;
        height: auto;
        padding-top: 15rpx;
    }
    

</style>
